<template>
  <div>
    <div>
      <van-nav-bar
        title="我的地址"
        left-text="返回"
        left-arrow
        @click-left="back()"
      />
      <van-address-list
        :list="list"
        default-tag-text="默认"
        @edit="getAid"
        @add="onAdd"
      />
      <van-empty
        v-show="addressnull"
        class="custom-image"
        image="https://img01.yzcdn.cn/vant/custom-empty-image.png"
        description="暂无地址信息"
      />
    </div>
  </div>
</template>

<script>
import { areaList } from "@vant/area-data";
export default {
  name: "AddressList",
  data() {
    return {
      addressnull: true,
      str: "",
      list: [],
      areaList: areaList,
      appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
      tokenString: this.$cookies.get("TOKEN"),
    };
  },
  created() {
    this.findAddress();
  },
  methods: {
    //返回我的页面
    back() {
      this.$router.push({ name: "Mine" });
    },
    onAdd() {
      this.$router.push({ name: "Editaddress" });
    },
    //点击获取地址aid
    getAid(e) {
      console.log(e);
      let a = e.aid;
      this.$router.push({ name: "Editaddress", query: { a, b: true } });
    },
    //查询地址
    findAddress() {
      this.axios({
        method: "get",
        url: "findAddress",
        params: {
          appkey: this.appkey,
          tokenString: this.tokenString,
        },
      }).then((res) => {
        res.data.result.forEach((item, index) => {
          console.log(item);
          item.id = index + 1;
          let str = `${item.province}${item.city}${item.county}${item.addressDetail}`;
          this.list.push(item);
          item.address = str;
        });
        //筛选默认地址
        this.list.forEach((item) => {
          if (item.isDefault == 1) {
            this.chosenAddressId = item.id;
            console.log("这是默认的地址==>", res);
            this.defaultAddress = item;
          }
        });
        if (res.data.result.length == 0) {
          this.addressnull = true;
        } else {
          this.addressnull = false;
        }
        console.log("这是地址信息==>", res);
      });
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.van-button--danger {
  background-color: #0c34ba;
  border: 1px solid #0c34ba;
}
/deep/.van-tag--danger {
  background-color: #0c34ba;
}
/deep/.van-address-list__bottom {
  background-color: rgba(0, 0, 0, 0);
}
/deep/.van-radio__icon {
  display: none;
}
</style>